//
//  DetailMakeSureTableViewCell.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/20.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "DetailMakeSureTableViewCell.h"
#import "StarView.h"
#import "DiscoverPhotoCollectionViewCell.h"

@interface DetailMakeSureTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *titleLabel; // 标题
@property (nonatomic, strong) UILabel *detailLabel; // 副标题
@property (nonatomic, strong) StarView *starView; // 小星星
@property(nonatomic,strong) UILabel *timeLabel;// 时间
@property(nonatomic,strong) UICollectionView * collectionView; // 图片
@property(nonatomic, strong) UICollectionViewFlowLayout * layout;
@property(nonatomic,strong) NSArray *dataArray;
@end

@implementation DetailMakeSureTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsZero;
        
        self.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.headerImageView];
        [self.contentView addSubview:self.titleLabel];
        
        __weak typeof(self)weakself = self;
        [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.mas_top).offset(10 / WIDTH_6S_SCALE);
            make.width.height.mas_offset(40 / WIDTH_6S_SCALE);
            make.left.equalTo(weakself.mas_left).offset(10 / WIDTH_6S_SCALE);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.headerImageView.mas_top);
            make.left.equalTo(weakself.headerImageView.mas_right).offset(5 / WIDTH_6S_SCALE);
            make.height.mas_offset(20 / HEIGHT_6S_SCALE);
        }];
        
        [self.contentView addSubview:self.starView];
        [self.starView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakself.titleLabel);
            make.top.equalTo(weakself.titleLabel.mas_bottom).offset(5 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(15/WIDTH_5S_SCALE);
            make.width.mas_equalTo(60/WIDTH_5S_SCALE);
        }];
        [self.contentView addSubview:self.detailLabel];
        [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakself.titleLabel.mas_left);
            make.right.equalTo(weakself).offset(-10 / WIDTH_6S_SCALE);
            make.top.equalTo(weakself.starView.mas_bottom).offset(5 / WIDTH_6S_SCALE);
        }];
        [self.contentView addSubview:self.timeLabel];
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakself).offset(-10 / WIDTH_6S_SCALE);
            make.centerY.equalTo(weakself.starView.mas_centerY);
            make.width.mas_equalTo(200 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
        }];
        [self.contentView addSubview:self.collectionView ];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakself).offset(55 / WIDTH_6S_SCALE);
            make.top.equalTo(weakself.detailLabel.mas_bottom).offset(15/WIDTH_5S_SCALE);
            make.right.equalTo(weakself.contentView).offset(-10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(0);
        }];
        UIView *lineView2 = [[UIView alloc]init];
        lineView2.backgroundColor = getColor(@"B4B0C0");
        [self.contentView addSubview:lineView2];
        [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakself.contentView.mas_left).offset(10 / WIDTH_6S_SCALE);
            make.right.equalTo(weakself.contentView.mas_right).offset(-10 / WIDTH_6S_SCALE);
            make.top.equalTo(weakself.contentView.mas_bottom).offset(0.5 / HEIGHT_6S_SCALE);
            make.height.mas_equalTo(0.5);
        }];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.headerImageView.mas_top).offset(-10 / HEIGHT_6S_SCALE);
            make.left.right.equalTo(weakself);
            make.bottom.equalTo(weakself.collectionView.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
        }];
        self.headerImageView.layer.cornerRadius = 20 / WIDTH_6S_SCALE;
        self.headerImageView.layer.masksToBounds = YES;
    }
    return self;
}
#pragma mark - init
- (UIImageView *)headerImageView
{
    if (!_headerImageView) {
        _headerImageView = [[UIImageView alloc]init];
        _headerImageView.backgroundColor = [UIColor clearColor];
        _headerImageView.image = [UIImage imageNamed:@"list_btn_pic.1"];
    }
    return _headerImageView;
}
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = getColor(blackColor);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = DEF_FontSize_14;
        [_titleLabel sizeToFit];
        _titleLabel.text = @"相识有缘304";
    }
    return _titleLabel;
}
- (UILabel *)detailLabel
{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.textColor = getColor(@"000000");
        _detailLabel.textAlignment = NSTextAlignmentLeft;
        _detailLabel.numberOfLines = 0;
        _detailLabel.font = DEF_FontSize_14;
        //[_detailLabel sizeToFit];
        _detailLabel.text = @"怀念你怀念你走了云的天空还任性是rtyuiop怀念你怀念你走了云的天空还任性是rtyuiop怀念你怀念你走了云的天空还任性是rtyuiop怀念你怀念你走了云的天空还任性是rtyuiop怀念你怀念你走了云的天空还任性是rtyuiop";
    }
    return _detailLabel;
}
- (StarView *)starView{
    if(!_starView){
        _starView = [[StarView alloc] initWithFrame:CGRectMake(_titleLabel.frame.origin.x, CGRectGetMaxY(_titleLabel.frame) + 10, 60/WIDTH_5S_SCALE, 15/WIDTH_5S_SCALE)];
        [_starView showMarkWithStar:4];
    }
    return _starView;
}
//时间
- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = DEF_FontSize_14;
        _timeLabel.textColor = getColor(textColor);
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.text = @"2018-01-02";
    }
    return _timeLabel;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        CGFloat width = (SCREEN_WIDTH - (55 / WIDTH_6S_SCALE + 10 / WIDTH_6S_SCALE + 12 + 10 / WIDTH_6S_SCALE * 2)) / 3.0;
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.itemSize = CGSizeMake(width, width);
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout.minimumLineSpacing = 6;
        _layout.minimumInteritemSpacing = 6;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[DiscoverPhotoCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([DiscoverPhotoCollectionViewCell class])];
    }return _collectionView;
}

#pragma mark - delegate和dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DiscoverPhotoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([DiscoverPhotoCollectionViewCell class]) forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}
//-(void)setModels:(NSDictionary *)models
- (void)setModels:(DetalMakeSureModelList *)models
{
    _models = models;
    self.titleLabel.text = models.username;
    self.detailLabel.text = models.content;
    self.timeLabel.text = models.commenttime;
    [_starView showMarkWithStar:[models.starnum intValue]];
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:models.avatar]];
    
    __weak __typeof(self)weakSelf = self;
//    NSArray * imageUrl = [ _models objectForKey:@"pic"];
    NSArray * imageUrl = models.commentimg;
    if (imageUrl.count > 0 || ![imageUrl isKindOfClass:[NSNull class]]) {
//        self.dataArray =[ _models objectForKey:@"pic"];
        self.dataArray = models.commentimg;
        CGFloat width = (SCREEN_WIDTH - (20 / WIDTH_5S_SCALE  + 55/WIDTH_5S_SCALE + 12 + 10 / WIDTH_6S_SCALE * 2)) / 3.0;
        NSInteger coutent = ceil(self.dataArray.count / 3.0);
        if (self.dataArray.count == 1) {
            _layout.itemSize = CGSizeMake(160, 160);
            [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(weakSelf.contentView).offset(50 / WIDTH_5S_SCALE);
                make.top.equalTo(weakSelf.detailLabel.mas_bottom).offset(15/WIDTH_5S_SCALE);
//                make.right.equalTo(weakSelf.contentView).offset(-20 / WIDTH_5S_SCALE);
                make.width.mas_equalTo(160);
                make.height.mas_equalTo(160);
            }];
        }
        else if (self.dataArray.count == 4)
        {
            _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
            _layout.itemSize = CGSizeMake(width, width);
            [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(weakSelf.contentView).offset(50 / WIDTH_5S_SCALE);
                make.top.equalTo(weakSelf.detailLabel.mas_bottom).offset(15/WIDTH_5S_SCALE);
                make.right.equalTo(weakSelf.contentView).offset(-20 / WIDTH_5S_SCALE - width - 5);
                make.height.mas_equalTo(width * 2 + 6);
            }];
        }
        else
        {
            _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
            _layout.itemSize = CGSizeMake(width, width);
            [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(weakSelf.contentView).offset(50 / WIDTH_5S_SCALE);
                make.top.equalTo(weakSelf.detailLabel.mas_bottom).offset(15/WIDTH_5S_SCALE);
                make.right.equalTo(weakSelf.contentView).offset(-20 / WIDTH_5S_SCALE + 1);
                make.height.mas_equalTo(width * coutent + (coutent - 1) * 6 + 1);
            }];
        }
        [self.collectionView reloadData];
    }
    else
    {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.contentView).offset(50 / WIDTH_5S_SCALE);
            make.top.equalTo(weakSelf.detailLabel.mas_bottom).offset(15/WIDTH_5S_SCALE);
            make.right.equalTo(weakSelf.contentView).offset(-20 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(0);
        }];
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.imagesblock) {
        NSMutableArray * viewsArray = [NSMutableArray array];
        NSMutableArray * imagesArray = [NSMutableArray array];
        for (int index = 0; index < _dataArray.count; index ++) {
            NSIndexPath * path = [NSIndexPath indexPathForItem:index inSection:0];
            DiscoverPhotoCollectionViewCell *cell = (DiscoverPhotoCollectionViewCell*)[collectionView cellForItemAtIndexPath:path];
            if (!cell.photoImg.image) {
                return;
            }
            [imagesArray addObject:_dataArray[index]];
            [viewsArray addObject:cell.photoImg];
        }
        self.imagesblock(imagesArray, indexPath.item, viewsArray);
    }
}
@end

